<?php 
//权限管理

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Menu;
use app\admin\model\Page;
use app\admin\model\Pagetype;
use app\admin\model\Role;
use think\Request;
use think\Config;
class Roles extends Base
{
	//角色管理
	public function index()
	{
	     //判断是否登录如果没有登录不能访问该方法
         $user = $this->user_info();
	     //查询所有角色
		 $res=Role::order("sort asc")->select();
		 //查询每个角色的权限名称和该角色下的用户
         foreach($res as $key=>$val){
             $str = "";
             if(!empty($val['jurisdiction'])){
                 //查询顶级栏目名称
                 $menu_name = Menu::where(array("pid"=>0,"id"=>array('in',$val['jurisdiction'])))->order("sort asc")->select();
                 foreach($menu_name as $k => $v){
                    $str .= "【".$v['title']."】:";
                     //查询二级栏目
                     $menu2_name = Menu::where(array("pid"=>$v['id'],"id"=>array('in',$val['jurisdiction'])))->select();
                     if(!empty($menu2_name)){
                         foreach($menu2_name as $ks => $vs){
                             $str .= "[".$vs['title']."]";
                         }
                     }
                     $str .= "<br/>";
                 }

             }else{
                 $str = "";
             }
             $res[$key]['jurisdiction2'] = $str;
             //查询该角色下的用户
             $user =  Admin::where(array("roles_id"=>$val['r_id']))->field(["username","photo"])->select();
             $res[$key]['user'] = $user;
             //查询当前角色的上级领导
             $parent = Role::where(array("r_id"=>$val['pid']))->field("r_name")->find();
             $res[$key]['parent'] = $parent['r_name'];

         }
         $data = array(
            "setting" => $this->getAllSetting(),
             "data"=>$res,

         );
         return view("index",$data);
	}
	//加载添加角色视图
	public  function add()
	{
	    //查询所有的角色
        $roleslist  = Role::all();
	    //权限
        $menu = Menu::where(array("status"=>1,"pid"=>0))->order("sort asc")->select();
        foreach($menu as $key => $val){
            $menutow = Menu::where(array("status"=>1,"pid"=>$val['id']))->select();
            $menutow = collection($menutow)->toArray();
            $menu[$key]['tow']=$menutow;
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "menu" =>$menu,
            "roleslist" => $roleslist,
        );
		return view("add",$data);
	}
	//执行添加
	public function toAdd(Request $request)
	{
		$data=$request->post();

		if (Role::insert($data)) {
            $this->success("添加成功","index");
		}else{
            $this->success("添加失败","add");
        }
	}

	//加载修改视图
	public  function updates(Request $request)
	{
	    //接收id
	    $id = $request->get("id");
	    //查询要修改的角色
	    $resource =  Role::where(array("r_id"=>$id))->find();
	    //查询所有角色
        $roleslist = Role::all();
	    //查询所有权限
        $menu = Menu::where(array("status"=>1,"pid"=>0))->order("sort asc")->select();
        foreach($menu as $key => $val){
            $menutow = Menu::where(array("status"=>1,"pid"=>$val['id']))->select();
            $menutow = collection($menutow)->toArray();
            $menu[$key]['tow']=$menutow;
        }
        $resource['jurisdiction'] = explode(",",$resource['jurisdiction']);
        $resource['resourceauthority'] = explode(",",$resource['resourceauthority']);

	    $data = array(
            "RA"=>Config::get('resource_authority'),//资源权限配置
	        "setting"=>$this->getAllSetting(),
            "data" => $resource,
            "menu"=>$menu,
            "roleslist" => $roleslist,
        );
	    return view("updates",$data);

	}
	//执行修改页面
	public function  toUpdates(Request $request)
	{

		if (Role::update($request->post())) {
			 $this->success("修改成功","index");
        }else{
            $this->error("修改失败","index");
        }
		

	}

    //执行删除
    public function del(Request $request){
        $id = $request->post("id");
        if(Role::destroy($id)){
            $this->success("删除成功","index");
        }else{
            $this->error("删除失败");
        }
    }
	
}
 ?>